<template>
  <dt-modal-edit
    ref="modal"
    title="新建奖金单"
    :label-width="130"
    :width="500"
    @ok="onOk"
    :loading="loading"></dt-modal-edit>
</template>

<script>
  import Opt from '../opt/NewOpt.js'
  import paramsMgr from '@/biz/bonus/params.js'
  import billMgr from '@/biz/bonus/bill'
  import extendMgr from '@/biz/bonus/bonusExtend.js'

  export default {
    data() {
      return {
        info: {
          bonusName: null,
          bonusType: 'STANDARD',
          bonusMonth: '',
          scaleType: null,
          reason: null
        },
        loading: false
      }
    },
    methods: {
      show() {
        paramsMgr.detail().then((res) => {
          extendMgr.getBoApplyMonth().then((resMonth) => {
            this.info.bonusMonth = resMonth
            this.$refs.modal.show(new Opt(this.info, res.isLockTime))
          })
        })
      },
      hide() {
        this.$refs.modal.hide()
      },
      onOk() {
        billMgr.name(this.info.bonusName).then((res) => {
          if (res) {
            this.loading = true
            billMgr
              .create(this.info)
              .then((res) => {
                this.$router.push({
                  path: `bill/edit`,
                  query: {
                    id: res,
                    type: this.info.bonusType
                  }
                })
                this.loading = false
              })
              .catch((err) => {
                this.loading = false
              })
          } else {
            this.$Message.error('该奖金单名称已存在，请重新输入。')
          }
        })
      }
    }
  }
</script>

<style></style>
